package com.lry.basic.algorithm.list;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2020/1/6
 */
public class MinStack {

    LinkedStack data = null;
    LinkedStack helper = null;


    public MinStack() {
        data = new LinkedStack();
        helper = new LinkedStack();
    }

    public void push(int x) {
        data.push(x);
        if(helper.isEmpty()||helper.peek()>x){
            helper.push(x);
        }else {
            helper.push(helper.peek());
        }
    }

    public void pop() {
        if(data!=null&&!data.isEmpty()&&helper!=null&&!helper.isEmpty()){
            data.pop();
            helper.pop();
        }
    }

    public int top() {
        if(data!=null&&!data.isEmpty()){
            return data.peek();
        }
        throw new RuntimeException("data stack is empty");
    }

    public int getMin() {
        if(helper!=null&&!helper.isEmpty()){
            return helper.peek();
        }
        throw new RuntimeException("helper stack is empty");
    }

}
